package InterviewTest.q0207_getIntersectionNode;
import CommonClass.Common.ListNode;
public class Solution {
    /*
    考虑两个链表长度 推算有交点时候 各自遍历到n + m长度就会相交
    所以用循环使两者走到链表末端时 接到另一个链表上 就可以判断是否有交点
     */
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if (headA == null || headB == null) return null;
        ListNode nodeA = headA, nodeB = headB;
        while (nodeA != nodeB) {
//            if (nodeA.next == nodeB.next) return nodeA.next;
//
//            nodeA = nodeA.next == null ? headB : nodeA.next;
//            nodeB = nodeB.next == null ? headA : nodeB.next;
            nodeA = nodeA == null ? headB : nodeA.next;
            nodeB = nodeB == null ? headA : nodeB.next;
        }

        return nodeA;
    }
}
